/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_frcmd_server_start.c                             */
/*  NOTE      =                                                    */
/*  DATE      = 2014/12/03 by geyh                                 */
/*******************************************************************/

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pthread.h>

#include "frcomm.h"
#include "frmem.h"
#include "frcmd_server.h"

FR_STATIC pthread_rwlock_t g_cmd_rwlock;

int __create_listen(unsigned short us_port, int *p_listen_sock);
int __send_data(int sockfd
                      , char *p_data
                      , unsigned int ui_data_len);


int main(int argc, char **argv)
{
    int i_ret = 0;
    int listen_sock = 0;
    int accept_sock = 0;
    int i_port = 6555;
    char *p_data = frmemdup("\x04\x00\x00\x00\x01\x02\x03",7);
    unsigned int ui_data_len = 7;

    /* 检查port值 */
    if (i_port <=0 || i_port > 65535)
    {
      //  FRLOG_ERROR_DETAIL("Port参数必须是1-65535的整数\n");
        return FR_FAIL;
    }

    i_ret = pthread_rwlock_init(&g_cmd_rwlock, NULL);
    if (i_ret != 0)
    {
     //   FRLOG_ERROR_DETAIL("pthread_rwlock_init failed\n");
        return FR_FAIL;
    }

    i_ret = __create_listen((unsigned short)i_port, &listen_sock);
    if (i_ret != 0)
    {
        pthread_rwlock_destroy(&g_cmd_rwlock);
        return FR_FAIL;
    }

    fd_set read_flags;

    while (1)
    {
        /* 接收来自服务器的响应信息，判断是否握手成功 */
        FD_ZERO(&read_flags);
        FD_SET(listen_sock, &read_flags);

        if(FD_ISSET(listen_sock, &read_flags))
        {
            accept_sock = accept(listen_sock, NULL, NULL);
            if(accept_sock < 0)
            {
            //    FRLOG_DEBUG_DETAIL("accept失败！\n");
                continue;
            }

        }

        send(accept_sock,p_data,ui_data_len,0);
        printf("%d\n",accept_sock);
        frfree(p_data);
        p_data = NULL;
        break;
    }
    p_data = frmemdup("\x06\x00\x00\x00\x62\x72\x6f\x61\x64\x61",10);
    ui_data_len = 10;
    while (1)
    {
        /* 接收来自服务器的响应信息，判断是否握手成功 */
        FD_ZERO(&read_flags);
        FD_SET(listen_sock, &read_flags);

        if(FD_ISSET(listen_sock, &read_flags))
        {
            accept_sock = accept(listen_sock, NULL, NULL);
            if(accept_sock < 0)
            {
            //    FRLOG_DEBUG_DETAIL("accept失败！\n");
                continue;
            }

        }

        send(accept_sock,p_data,ui_data_len,0);
        printf("%d\n",accept_sock);
        frfree(p_data);
        p_data = NULL;
        accept_sock = accept(listen_sock, NULL, NULL);
    }

    close(listen_sock);
    pthread_rwlock_destroy(&g_cmd_rwlock);

    return FR_SUCC;

}